﻿using Assets.Core.Common.Web;
using Assets.Core.Jwt;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Reflection;

namespace Nucleic.Web.Filters
{
    public class ApiAuthorityAttribute : IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            var controller = context.RouteData.Values["controller"].ToString();
            var action = context.RouteData.Values["action"].ToString();
            if (context.ActionDescriptor.GetType().GetCustomAttributes(typeof(AllowAnonymousAttribute)).Any())
            {
                return;
            }
            if (!context.ActionDescriptor.GetType().GetCustomAttributes(typeof(CheckPermissionAttribute)).Any())
            {
                return;
            }
            var token = context.HttpContext.Request.Headers["token"].ToString();
            var result= JwtHelp.CheckToken(token);
            if (result.Success)
            {

            }
            else
            {
                var ret= new JsonResult(Result.fail("token认证失败"));
                ret.StatusCode = 200;
                context.Result = ret;
            }
        }
    }
}
